<?
include_once "./languageDetector.php";
include_once "./Normalizer.php";
include_once "./SmileLaughDetection.php";


class Creator {
	
	public function __construct() {}
	
	public function getSentyPosts($MrXPosts, $mrX) {
		
		/* Language Detection */
		$language = new LangDetector();
		$normalizer = new Normalizer();
		$smileLaughDetector = new SmileLaughDetector();
	
		$all_senty_post = array();

		foreach($MrXPosts as $post)
		{		
			// add the message to the new SentyPost obj, but only if it not empty and it is in english
			$message = false;
			$tmp = $post -> message;
			if($tmp && $language->detect($tmp) == "english")
			{
				//let's consider this message
				$message = $tmp;
	
				// find smiles in the message and remove them
				$result = $smileLaughDetector -> findSmiles($post -> message);
				$pos_smiles = $result[1];
				$neg_smiles = $result[2];
				$message = $result[0];
	
				// normalize the message 
				$result = $normalizer -> normalize($message, $dict);
			
				// construct the new message
				$message = array();
				$message['sentences'] = array();
				foreach ($result as $sentences){
					array_push($message['sentences'], array('text' => $sentences, 's_value' => 0));
				}
				
				// get the sentiment given from the smiles and add it into the obj
				// get the sentiment given from the smiles and add it into the obj
				$smile_value = 5 - $neg_smiles + $pos_smiles;
				if ($smile_value < 0)
					$smile_value = 0;
				else if ($smile_value > 10)
					$smile_value = 10;
				// if there are no smiles in the comment put -1.
				if ($pos_smiles + $neg_smiles > 0)
					$message['smiles'] = $smile_value;
				else
					$message['smiles'] = -1;
					
//				echo 'MESSAGE:<br><pre>'.print_r($message, true).'</pre>';
			}
			
	
			// add english comments to the new SentyPost obj
			$comments = array();
			foreach ($post-> comments as $current_comment)
			{
				$tmp = $current_comment['message'];
				if($language->detect($tmp) == "english")
				{
					//let's consider this comment
					$current_comment_mex = $tmp;
		
					// find smiles in the message and remove them
					$result = $smileLaughDetector -> findSmiles($current_comment_mex);
					$pos_smiles = $result[1];
					$neg_smiles = $result[2];
					$current_comment_mex = $result[0];
	
					// normalize the message 
					$result =  $normalizer -> normalize($current_comment_mex, $dict);
				
					// construct the new message
					$current_comment_mex = array();
					$current_comment_mex['sentences'] = array();
					
					// add the sentences (if after normalization there still are any)
					foreach ($result as $sentences){
						if (count($sentences) > 0)
							array_push($current_comment_mex['sentences'], array('text' => $sentences, 's_value' => 0));
					}
					
					// get the sentiment given from the smiles and add it into the obj
					$smile_value = 5 - $neg_smiles + $pos_smiles;
					if ($smile_value < 0)
						$smile_value = 0;
					else if ($smile_value > 10)
						$smile_value = 10;
					// if there are no smiles in the comment put -1.
					if ($pos_smiles + $neg_smiles > 0)
						$current_comment_mex['smiles'] = $smile_value;
					else
						$current_comment_mex['smiles'] = -1;
					
					if ($current_comment_mex['sentences'] > 0 || $current_comment_mex['smiles'] != -1)
						array_push($comments, $current_comment_mex);
				}
//				echo 'COMMENTS:<br><pre>'.print_r($comments, true).'</pre>';
			}
			
			if ($message || count($comments) > 0 || $current_comment['numLikes'] > 0) {
				$s_post_to_add = new SentyPost();
				$s_post_to_add -> id = 	$post -> id;
				$s_post_to_add -> created_time = $post -> created_time;
				$s_post_to_add -> message = $message;
				$s_post_to_add -> comments = $comments;
				
				if ($post -> numLikes > 0)
					$s_post_to_add -> numLikes = $post -> numLikes;
				else
					$s_post_to_add -> numLikes = 0;
				
//				echo 'ADDING POST <pre>'.print_r($s_post_to_add, true).'</pre>';
				array_push($all_senty_post, $s_post_to_add);
			}
		}

		return $all_senty_post;
	}
	
	
	
}



?>